package com.atguigu.gulimall.member.controller;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.gulimall.member.excepttion.PhoneExistException;
import com.atguigu.gulimall.member.excepttion.UsernameExistException;
import com.atguigu.gulimall.member.feign.CouponFeignService;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import org.apache.catalina.User;
import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream;
import org.omg.CORBA.WCharSeqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
/**
 * 会员
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-10-08 09:47:05
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    CouponFeignService couponFeignService;
@PostMapping("/regist")
    public  R regist(@RequestBody  MemberRegistVo vo){
    try {
        memberService.regist(vo);
    }catch (PhoneExistException e){
     return  R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg())   ;
    }catch (UsernameExistException e){
        return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(),BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
    }
     return R.ok();
    }
    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupons();
        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);
        return R.ok();
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);
        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
    @PostMapping("login")
public  R login(@RequestBody MemberLoginVo vo){



        //   在这里 没有 进行  session操作,  进行session操作的  是   auth  服务



        MemberEntity member=memberService.login(vo);
        if(member==null){
            return R.error(BizCodeEnume.LOGINACCOUNT_PASSWORD_INVAIlD_EXCEPTION.getCode(), BizCodeEnume.LOGINACCOUNT_PASSWORD_INVAIlD_EXCEPTION.getMsg());
        }else{


            HashMap<String, Object> map = new HashMap<>();

            map.put("data",member);


        return  R.ok(map);


        }
}
}
